﻿Imports System.IO
Imports System.Windows.Forms

Public Class EditorThemeOptionPage
    Inherits OptionPage
    Friend WithEvents ThemePreviewBox As System.Windows.Forms.PictureBox
    Friend WithEvents ThemeSelectionBox As System.Windows.Forms.ListBox

    Private Const kThemeDir As String = "themes\"

    Public Sub New()
        InitializeComponent()
    End Sub

    Public Overrides Sub OnApplySettings()
        Dim themename As String = kThemeDir + ThemeSelectionBox.SelectedItem.ToString().ToLower() + ".xml"
        MainForm.EditorTabManager.ChangeThemes(themename)

        Globals.EditorSettings.SetTrait("EditorTheme", themename)
    End Sub

    Public Overrides Sub OnLoadSettings()
        LoadThemeList()
    End Sub

    Public Overrides Sub OnFocus()
        ThemeSelectionBox.Focus()
    End Sub

    Public Sub LoadThemeList()
        ThemeSelectionBox.Items.Clear()

        Dim dir As New DirectoryInfo(kThemeDir)
        For Each themefile In dir.GetFiles()
            If themefile.Name.EndsWith(".xml") Then
                Dim themename As String = themefile.Name.Replace(".xml", "")
                themename = themename.ToUpper()(0) + themename.Substring(1, themename.Count - 1)
                ThemeSelectionBox.Items.Add(themename)
            End If
        Next

        Dim lasttheme As String = Globals.EditorSettings.GetTrait("EditorTheme", kThemeDir + "dark.xml")
        For i As Integer = 0 To ThemeSelectionBox.Items.Count - 1
            ' Select the first element by default
            Dim themename As String = kThemeDir + ThemeSelectionBox.Items(i).ToString().ToLower() + ".xml"
            If themename = lasttheme Then
                ThemeSelectionBox.SelectedIndex = i
            End If
        Next
    End Sub

    Private Sub ThemeSelectionBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ThemeSelectionBox.SelectedIndexChanged
        ' Change the Preview Picture according to the selected theme

        Dim themepic As String = kThemeDir + ThemeSelectionBox.SelectedItem.ToString().ToLower() + ".png"

        Try
            ThemePreviewBox.Image = Image.FromFile(themepic)
        Catch ex As Exception
            ' Ignore 
            ThemePreviewBox.Image = Nothing
        End Try
    End Sub

    Private Sub InitializeComponent()
        Me.ThemeSelectionBox = New System.Windows.Forms.ListBox()
        Me.ThemePreviewBox = New System.Windows.Forms.PictureBox()
        CType(Me.ThemePreviewBox, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'ThemeSelectionBox
        '
        Me.ThemeSelectionBox.FormattingEnabled = True
        Me.ThemeSelectionBox.Location = New System.Drawing.Point(10, 10)
        Me.ThemeSelectionBox.Name = "ThemeSelectionBox"
        Me.ThemeSelectionBox.Size = New System.Drawing.Size(400, 400)
        Me.ThemeSelectionBox.TabIndex = 0
        '
        'ThemePreviewBox
        '
        Me.ThemePreviewBox.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.ThemePreviewBox.Location = New System.Drawing.Point(430, 10)
        Me.ThemePreviewBox.Name = "ThemePreviewBox"
        Me.ThemePreviewBox.Size = New System.Drawing.Size(450, 390)
        Me.ThemePreviewBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
        Me.ThemePreviewBox.TabIndex = 0
        Me.ThemePreviewBox.TabStop = False
        '
        'EditorThemeOptionPage
        '
        Me.Controls.Add(Me.ThemePreviewBox)
        Me.Controls.Add(Me.ThemeSelectionBox)
        Me.Text = "Themes"
        CType(Me.ThemePreviewBox, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

End Class
